package cst.a110lyj.fragmenttest110lyj.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import cst.a110lyj.fragmenttest110lyj.R;
import cst.a110lyj.fragmenttest110lyj.iface.AmicoolListener;
import cst.a110lyj.fragmenttest110lyj.model.AmicoolModel;


/**
 * Created by Administrator on 2017/5/26/0026.
 */

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText username;
    private EditText password;
    private EditText tel;
    private EditText repassword;
    private Button btn_confirm;
    private Button btn_return;
    private AmicoolModel model;
    private AmicoolListener<String> listener;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        init();
        btn_confirm.setOnClickListener(this);
        btn_return.setOnClickListener(this);
        listener = new AmicoolListener<String>(){
            @Override
            public void onResponse(String l) {
                if (l.equals("1")) {
                    Toast.makeText(RegisterActivity.this, "注册成功，返回登陆", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                    startActivity(intent);
                    finish();
                } else
                    Toast.makeText(RegisterActivity.this, "用户已经存在", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFail(String msg) {
                Toast.makeText(RegisterActivity.this, msg, Toast.LENGTH_SHORT).show();
            }
        };
    }


    private void init() {
        username = (EditText) findViewById(R.id.et_username);
        password = (EditText) findViewById(R.id.et_password);
        tel = (EditText) findViewById(R.id.et_tel);
        repassword = (EditText) findViewById(R.id.et_rePassword);
        btn_confirm = (Button) findViewById(R.id.btn_register);
        btn_return = (Button) findViewById(R.id.btn_return);
        //sp=getSharedPreferences("login",MODE_PRIVATE);//第一个参数：文件名，第二个参数：模式
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_register:
                String tmpuser = username.getText().toString();
                String tmppass = password.getText().toString();
                String tmtel = tel.getText().toString();
                model = new AmicoolModel ("string");
                model.reg(tmpuser,tmppass,tmtel,listener);
                break;
            case R.id.btn_return:
                Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                startActivity(intent);
                finish();
        }
    }

}